package ru.elteam.ejb.util;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author Nikita.Brazhnikov
 */
public class Debug {

	private static final Debug instance = new Debug();

	private Debug() {
	}

	public static <T> Debug prints(String msg, Collection<T> col) {
		return instance.print(msg, col);
	}

	public static Debug prints(String msg, Object object) {
		System.out.printf("Log: %s: \n %s", msg, ObjectUtils.toString(object, "null"));
		return instance;
	}

	public <T> Debug print(String msg, Collection<T> col) {
		StringBuffer sb = new StringBuffer();
		sb.append("Log: ");
		sb.append(msg);
		sb.append(": \n");
		if (col == null)
			sb.append("null");
		else {
			sb.append("[");
			List<String> strings = new ArrayList<String>(col.size());
			for (T obj : col) {
				strings.add(ObjectUtils.toString(obj, "null"));
			}
			sb.append(StringUtils.join(strings, ", "));
			sb.append("]");
		}
		System.out.println(sb.toString());
		return instance;
	}
}
